'##################################################
'###  Top Level (Get root Path from system env)
'##################################################
function GetEnv(envVarName)
	On Error Resume Next
	' 创建Shell对象并获取值
	Set WshShell = CreateObject("WScript.Shell")
	envValue = WshShell.Environment("PROCESS")(envVarName) ' 优先查找进程级变量

	' 如果未找到则尝试系统级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("SYSTEM")(envVarName)
	End If

	' 如果仍未找到则尝试用户级变量
	If Err.Number <> 0 Or envValue = "" Then
		Err.Clear
		envValue = WshShell.Environment("USER")(envVarName)
	End If

	' 检查结果并输出
	If Err.Number = 0 And envValue <> "" Then
		GetEnv=envValue
	Else
		GetEnv=""
	End If
end function

'##################################################
'##################################################
'##################################################
sub IncludeVbs(filepath)
	Dim fso, file, content
	Set fso = CreateObject("Scripting.FileSystemObject")
	' 读取functions.vbs的内容
	Set file = fso.OpenTextFile(filepath, 1) ' 1表示只读
	content = file.ReadAll
	file.Close
	' 将functions.vbs的内容载入全局作用域
	ExecuteGlobal content
end sub

'##################################################
'##################################################
'##################################################
dim root,tmpfile
Dim doc
Set doc = application.ActiveDocument
main
set doc = nothing
sub main()	
	'// REUSE
	envVarName ="EXP_SHORTCUTS_ROOT"
	root = GetEnv(envVarName)'"C:\MentorGraphics\2005EXP\SDD_HOME\pvtk\env\"
	if root ="" then 
		msgbox "please set env value (EXP_SHORTCUTS_ROOT = ?)"
		exit sub
	end if
	
	call IncludeVbs(root & "\skill_lib.vbs")
	
	If ValidateServer(doc) = 0 Then
	    msgbox "Server validation failed."
	    exit sub
	End If	
	
	'// TODO
	call IncludeVbs(root & "skill\includes\ClipboardHelper.vbs")
	Set clipboard = New ClipboardHelper
	args = clipboard.GetText()
	Set clipboard = nothing

	args = ucase(trim(args))
	if args ="" then exit sub
	
	
	dim cs,s,ls,i
	cs = split(args," ")
	dim ref:ref = cs(0)
	dim pinstr:pinstr = cs(1)
	
	s = split(pinstr,",")
	ls = ubound(s)-lbound(s)+1
	msgbox ls
	dim compobj:set compobj = doc.FindComponent(ref)
	if compobj is nothing then exit sub
	
	dim dict
	set dict = createobject("scripting.dictionary")

	dim selnet
	dim flag
	for i=0 to ls-1
		dim selpin :selpin = s(i)
		dim pinObj:set pinObj = compobj.FindPin(selpin)
		if not pinObj is nothing then
			selnet = pinObj.net.name
			if dict.Exists(selnet)=false then
				call dict.add(selnet,"")
			end if					
		end if			
	next
	
	dim ostr:ostr=""
	for each okey in dict
		if ostr<>"" then ostr = ostr & vbCrlf
		ostr = ostr & okey
	next
	dict.removeall
	set dict = nothing
	
	msgbox ostr	
end sub
